package com.youlian.cloud.log.utils;

import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @describe ：请求工具类
 * @Author ： liukx
 * @time ： 2019/1/25 - 11:46
 */
public class RequestUtils {

    /**
     * 获取传递进来不为空的返回值
     *
     * @param str
     * @return
     */
    public static String getPriorityNotNull(String... str) {
        if (str != null && str.length > 0) {
            for (int i = 0; i < str.length; i++) {
                String val = str[i];
                if (!StringUtils.isEmpty(val)) {
                    return val;
                }
            }
        }
        return null;
    }

    /**
     * 获取客户端编号
     *
     * @param req
     * @return
     */
    public static String getClientIP(HttpServletRequest req) {
        return getPriorityNotNull(req.getHeader("remoteip"), req.getHeader("x-forwarded-for"), req.getRemoteAddr());
    }

    /**
     * 获取客户端设备
     *
     * @param req
     * @return
     */
    public static String getUserAgent(HttpServletRequest req) {
        String userAgent = req.getHeader("user-agent");
        return userAgent;
    }

    /**
     * 获取客户端来源
     *
     * @param req
     * @return
     */
    public static String getReferer(HttpServletRequest req) {
        String referer = req.getHeader("referer");
        return referer;
    }

    /**
     * 获取请求中的操作系统
     *
     * @param req
     * @return
     */
    public static String getSystem(HttpServletRequest req) {
        String userAgent = getUserAgent(req);
        if (!StringUtils.isEmpty(userAgent)) {
            if (userAgent.indexOf(CatMsgConstants.AGENT_ANDROID) > 0) {
                return CatMsgConstants.AGENT_ANDROID;
            } else if (userAgent.indexOf(CatMsgConstants.AGENT_IPHONE) > 0) {
                return CatMsgConstants.AGENT_IPHONE;
            }
        }
        return userAgent;
    }

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>(1);
        map.put("1", "2");
        map.put("1", "2");
        map.put("1", "2");
        map.put("1", "2");
        map.put("1", "2");
        map.put("1", "2");
    }

}
